import os
import glob
from string import letters
import sys

if len(sys.argv) < 3:
    print "Not enough arguments."
    sys.exit()

letter = sys.argv[1]

# specify feature executables

#features = ['corners', 'convexity', 'complexity', 'diffsegs', 'projection', 'endpoints', 'ratio']
features = ['corners', 'convexity', 'complexity', 'diffsegs', 'endpoints', 'ratio', 'holes', 'axismean']
#features = ['axismean']

output = []
labels = []
labeled = False

# iterate over letters in specified directory
for (j, letter) in enumerate(glob.glob(os.path.join(sys.argv[1], '*.png'))):
    if j > 0:
        labeled = True
    output.append([])
    output[j].append(os.path.splitext(os.path.basename(letter))[0]) # append letter as class
    # iterate over each feature, run program and save output
    for (i, feature) in enumerate(features):
        fc = 0
        p = os.popen('./{0} {1}'.format(feature, letter))
        for line in p.readlines():
            for f in line.split(", "):
                output[j].append(f.strip())
                if labeled == False:
                    labels.append("{0}{1}".format(feature, fc))
                fc = fc + 1

f = open(sys.argv[2], 'w')
f.write("letter, {0}\n".format(", ".join(labels)))

for letter in output:
    f.write("{0}\n".format(", ".join(letter)))
